package cn.xuhang.nav.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

public class DaoImpl<T> implements Dao<T> {
	@Autowired
    public SqlSession sqlSession;
	
	public Class protoClass;
	public String namespace;
	public String ClassName;
	public DaoImpl(){
		this.protoClass = getProtoClass();
		this.ClassName = this.protoClass.getName().substring(this.protoClass.getName().lastIndexOf(".")+1);
		this.namespace = "cn.xuhang.nav.mapper.";
	}
	
	
	private Class getProtoClass() {
		ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
		Class clazz = (Class) type.getActualTypeArguments()[0];
		return clazz;
	}


	public void insert(T t) {
		this.sqlSession.insert(this.namespace+"insert"+this.ClassName,t);
	}


	public List<T> query() {
		return this.sqlSession.selectList(this.namespace+"query"+this.ClassName);
	}


	public T queryById(Serializable id) {
		return this.sqlSession.selectOne(this.namespace+"queryById", id);
	}


	public void update(T t) {
		this.sqlSession.update(this.namespace+"update"+this.ClassName, t);
		
	}


	public void delete(Serializable id) {
		this.sqlSession.delete(this.namespace+"delete"+this.ClassName, id);
	}
	

}
